package com.danco.training.zaneuskaya.connection;

import java.sql.Connection;

import org.apache.log4j.Logger;

import com.danco.training.zaneuskaya.property.DBPropertyAccess;


import java.sql.DriverManager;
import java.sql.SQLException;



public class DBConnection {
	public String url = DBPropertyAccess.getDBProperty("url");
	public String user = DBPropertyAccess.getDBProperty("user");
	public String password = DBPropertyAccess.getDBProperty("password"); 
	
	public String driver = DBPropertyAccess.getDBProperty("driver"); 
	
	private static final Logger logger = Logger.getLogger(DBConnection.class);

	private static DBConnection dBConnectionInstance;

	private DBConnection() {
		
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			logger.error(e.getMessage(), e);
		}
	}

	public Connection createConnection() {

		
		Connection connection = null;
		try {
			connection = DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			logger.error(e.getMessage(), e);
		}

		return connection;

	}

	public void closeConnection(Connection connection) {
		try {
			if (connection != null)
				connection.close();
		} catch (SQLException e) {
			logger.error(e.getMessage(), e);
		}
	}

	public static DBConnection getDBConnectionInstance() {
		if (dBConnectionInstance == null) {
			dBConnectionInstance = new DBConnection();
		}
		return dBConnectionInstance;

	}

}
